import {
    get,
    post,
    put,
    del,
    customPost,
    API_URL,
    HOST,
    PORT
} from "./http";
import {
    Base64
} from 'js-base64';

export const UPLOAD_BASE_URL = API_URL;

export const PREVIEW_URL = 'http://' + HOST + ':8012/onlinePreview?url='

//  base method

export const baseGet = (url, params) => get(url, params);

export const basePost = (url, params) => post(url, params);

export const basePut = (url, params) => put(url, params);

export const baseCustomPost = (url, params, headers, progressEvent) => post(url, params, headers, progressEvent);

// user api

export const login = params => post('/users/login', params);

export const getAllUserInfo = (url, params) => get(url, params);

export const getAllUserList = params => get('/users', params);

export const getAllUserPage = (currentPage, pageSize, params) => get('/users/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const searchUserPage = (currentPage, pageSize, params) => get('/users/search/query/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const saveUser = (params) => post('/users/save', params);

export const register = params => post('/users/register', params);



export const uploadFile = params => post('/attachment/upload', params);


// company api

export const getAllCompanyList = params => get('/companies', params);

export const searchCompanyPageByQuery = (currentPage, pageSize, params) => get('/companies/search/query/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const saveCompany = (params) => customPost('/companies/save', params);

export const deleteCompanyById = (id) => post('/companies/delete/companyId/' + id);

// depart api 

export const getAllDepartList = params => get('/departs', params);

export const getDepartPage = (currentPage, pageSize, params) => get('/departs/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const searchDepartPage = (currentPage, pageSize, params) => get('/departs/search/query/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const saveDepart = (params) => customPost('/departs/save', params);

export const deleteDepartById = (id) => post('/departs/delete/departId/' + id);




// contractType api

export const getAllContractTypeList = params => get('/contracts/types', params);

export const getContractTypePage = (currentPage, pageSize, params) => get('/contracts/types/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const searchContractTypePage = (currentPage, pageSize, params) => get('contracts/types/search/query/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const saveContractType = (params) => customPost('/contracts/types/save', params);

export const deleteContractTypeById = (id) => post('/contracts/types/delete/id/' + id);


// attachment api
export const getAttachmentPageByContractId = (contractId, currentPage, pageSize, params) => get('/contracts/' + contractId + '/attachments/search/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const saveAttachmentByContractId = (contractId, params, progressEvent) => customPost('/contracts/' + contractId + '/attachments/upload', params, {
    'Content-Type': 'multipart/form-data'
}, progressEvent);

export const viewAttachmentByContractIdAndAttachId = (contractId, attachId, params) => get('/contracts/' + contractId + '/attachments/view/' + attachId, params);

export function getAttachmentDownloadURL(contractId, attachId, attachName) {
    return UPLOAD_BASE_URL + '/contracts/' + contractId + '/attachments/view/' + attachId + '?fullfilename=' + attachName;
};

export function getAttachmentViewURL(contractId, attachId, attachName) {
    let previewURL = getAttachmentDownloadURL(contractId, attachId, attachName);
    return PREVIEW_URL + encodeURIComponent(Base64.encode(previewURL));
};

export const delAttachmentByContractidAndAttachId = (contractId, attachId, params) => del('/contracts/' + contractId + '/attachments/' + attachId, params);


export const getAllAttachmentList = params => get('/attachments', params);


// status api

export const getStatusPageByContractId = (contractId, currentPage, pageSize, params) => get('/contracts/' + contractId + '/statuses/query/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const saveContractStatus = (contractId, params) => customPost('/contracts/' + contractId + '/statuses', params, {
    'Content-Type': 'application/json'
});

export const deleteContractStatusByStatusId = (contractId, statusId) => del('/contracts/' + contractId + '/statuses/' + statusId);

export const getAllContractStatusList = params => get('/contracts/statuses', params);

// contract api

export const saveContract = (params, headers, progressEvent) => customPost('/contracts/save', params, headers, progressEvent);

export const searchContract = (currentPage, pageSize, params) => get('/contracts/dynamic/currentPage/' + currentPage + '/pageSize/' + pageSize, params);

export const getContractById = (contractId, params) => get('/contracts/id/' + contractId, params);

export const getAllContractInfo = params => get('/contracts', params);

export const deleteContractById = id => post('/contracts/delete/id/'+id);




// template api
export const searchAllPageByQuery = (currentPage, pageSize, params) => get('/templates/search/query/currentPage/' + currentPage + '/pageSize/' + pageSize, params)

export const saveTemplate = (params) => customPost('/templates/save',params);

export const deleteTemplateById = (id) => post('/templates/delete/'+id);

export const getAllContractTemplateList = params => get('/templates', params);

export const getTempaltePage = (currentPage, pageSize, params) => get('/templates/currentPage/' + currentPage + '/pageSize/' + pageSize, params);
